//
//  DareMemoryAppDelegate.m
//  DareMemory
//
//  Created by caozp on 09-11-30.
//  Copyright pla 2009. All rights reserved.
//

#import "DareMemoryAppDelegate.h"
#import "cocos2d.h"
#import "MainScene.h"

int nNumberLevel = 1;
int nColorLevel = 1;
BOOL isSound = YES;
BOOL isMusic = YES;
BOOL isNormal = YES;

@implementation DareMemoryAppDelegate

@synthesize window;

- (void) applicationDidFinishLaunching:(UIApplication*)application
{
	//读文件
	NSString *filePath = [self GetFilePath];
	if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
		NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
		NSString *numberLevel = [array objectAtIndex:0];
		NSString *colorLevel = [array objectAtIndex:1];
		NSString *sound = [array objectAtIndex:2];
		NSString *music = [array objectAtIndex:3];
		NSString *normal = [array objectAtIndex:4];
		
		nNumberLevel = [numberLevel intValue];
		nColorLevel = [colorLevel intValue];
		isSound = [sound boolValue];
		isMusic = [music boolValue];
		isNormal = [normal boolValue];
		
		[numberLevel release];
		[colorLevel release];
		[sound release];
		[music release];
		[normal release];
	}
	else {
		nNumberLevel = 1;
		nColorLevel = 1;
		isSound = YES;
		isMusic = YES;
		isNormal = YES;
	}

	// Init the window
	window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
	
	// cocos2d will inherit these values
	[window setUserInteractionEnabled:YES];	
	[window setMultipleTouchEnabled:YES];
	
	// Try to use CADisplayLink director
	// if it fails (SDK < 3.1) use Threaded director
	if( ! [Director setDirectorType:CCDirectorTypeDisplayLink] )
		[Director setDirectorType:CCDirectorTypeDefault];
	
	// Use RGBA_8888 buffers
	// Default is: RGB_565 buffers
	[[Director sharedDirector] setPixelFormat:kPixelFormatRGBA8888];
	
	// Create a depth buffer of 16 bits
	// Enable it if you are going to use 3D transitions or 3d objects
//	[[Director sharedDirector] setDepthBufferFormat:kDepthBuffer16];
	
	// Default texture format for PNG/BMP/TIFF/JPEG/GIF images
	// It can be RGBA8888, RGBA4444, RGB5_A1, RGB565
	// You can change anytime.
	[Texture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA8888];
	
	// before creating any layer, set the landscape mode
	[[Director sharedDirector] setDeviceOrientation:CCDeviceOrientationLandscapeLeft];
	[[Director sharedDirector] setAnimationInterval:1.0/60];
	[[Director sharedDirector] setDisplayFPS:NO];
	
	// create an openGL view inside a window
	[[Director sharedDirector] attachInView:window];	
	[window makeKeyAndVisible];		
		
		
	[[Director sharedDirector] runWithScene: [MainScene scene]];
}


- (void)applicationWillResignActive:(UIApplication *)application {
	[[Director sharedDirector] pause];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
	[[Director sharedDirector] resume];
}

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
	[[TextureMgr sharedTextureMgr] removeUnusedTextures];
}

- (void)applicationWillTerminate:(UIApplication *)application {
	//写文件
	NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:5];
	
	NSString *stringNumberLevel = [[NSString alloc] initWithFormat:@"%d", nNumberLevel];
	[array addObject:stringNumberLevel];
	
	NSString *stringColorLevel = [[NSString alloc] initWithFormat:@"%d", nColorLevel];
	[array addObject:stringColorLevel];
	
	if (isSound == YES) {
		[array addObject:@"YES"];
	}
	else {
		[array addObject:@"NO"];
	}

	if (isMusic == YES) {
		[array addObject:@"YES"];
	}
	else {
		[array addObject:@"NO"];
	}
	
	if (isNormal == YES) {
		[array addObject:@"YES"];
	}
	else {
		[array addObject:@"NO"];
	}

	[array writeToFile:[self GetFilePath] atomically:YES];
	[array release];
	
	[[Director sharedDirector] end];
}

- (void)applicationSignificantTimeChange:(UIApplication *)application {
	[[Director sharedDirector] setNextDeltaTimeZero:YES];
}

- (void)dealloc {
	[[Director sharedDirector] release];
	[window release];
	[super dealloc];
}

- (NSString *) GetFilePath
{
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentDirectory = [paths objectAtIndex:0];
	return [documentDirectory stringByAppendingPathComponent:@"data.plist"];
}

@end
